﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using DevExpress.XtraEditors;


namespace QLNSClient
{
    public partial class uc_ThemQDNgachBacHeso : UserControl
    {

        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public uc_ThemQDNgachBacHeso()
        {
            InitializeComponent();
        }

        

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Frm_ThemBL fr= new Frm_ThemBL();
            fr.ShowDialog();
            lke_MaCB.Properties.DataSource = sv.getCB();
        }


        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Luu.Enabled = true;
            btn_Them.Enabled = true;

            string tempMa_CB = "";
            if (lke_MaCB.EditValue.ToString() == "")
            {
                tempMa_CB = null;
                MessageBox.Show("Bạn chưa chọn cán bộ!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lke_MaCB.Focus();
            }
            else
            {
                tempMa_CB = lke_MaCB.EditValue.ToString();
            }

            string tempMa_Ngach = "";
            if (lke_MaNgach.EditValue.ToString() == "")
            {
                tempMa_Ngach = null;
                MessageBox.Show("Bạn chưa chọn cán bộ!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lke_MaNgach.Focus();
            }
            else
            {
                tempMa_Ngach = lke_MaNgach.EditValue.ToString();
            }

            string tempBLHienTai = txt_BLHienTai.Text;
            string tempMa_BL = "";
            if (lke_MaBL.EditValue.ToString() == "")
            {
                tempMa_BL = null;
                MessageBox.Show("Bạn chưa chọn bậc lương tiếp theo!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                lke_MaBL.Focus();
            }
            else
            {
                tempMa_BL = lke_MaBL.EditValue.ToString();
            }
                    string temp_Ma_HS_Luong = tempHSL.ToString();
                    string tempQuyetDinh = txt_QuyetDinh.Text;
            DateTime tempNgayKy;
            if (de_NgayKy.Text == "")
            {
                tempNgayKy = DateTime.Today.Date;
            }
            else
            {
                tempNgayKy = de_NgayKy.DateTime;
            }
            string tempNoiDung = txtNoiDung.Text;
            DateTime tempNgayHieuLuc;
            if (de_NgayHieuLuc.Text == "")
            {
                tempNgayHieuLuc = DateTime.Today.Date;
            }
            else
            {
                tempNgayHieuLuc = de_NgayHieuLuc.DateTime;
            }

            if (tempMa_CB != null && tempMa_Ngach != null && tempMa_BL != null)
            {
                QLNSServiceReference.CANBO_NGACH_LUONG cbnl = new QLNSServiceReference.CANBO_NGACH_LUONG();
                cbnl.Ma_CB = tempMa_CB;
                cbnl.Ma_Ngach = tempMa_Ngach;
                cbnl.Ma_BL = Int32.Parse(tempMa_BL);
                cbnl.QuyetDinh = tempQuyetDinh;
                cbnl.NoiDung = tempNoiDung;
                cbnl.NgayKy = tempNgayKy;
                cbnl.NgayHieuLuc = tempNgayHieuLuc;

                ////Lay ten CB 
                //List<QLNSServiceReference.CAN_BO> ListTenCBtheoMa_Cb = new List<QLNSServiceReference.CAN_BO>(sv.getTenCBtheoMaCB(tempMa_CB));
                //string tempHoTen = ListTenCBtheoMa_Cb[0].HoTen;

                QLNSServiceReference.HSL_HIENTAI hslht = new QLNSServiceReference.HSL_HIENTAI();
                hslht.Ma_CB = tempMa_CB;
                hslht.HoTen = lke_MaCB.Text;
                hslht.Ten_Ngach = tempMa_Ngach;
                hslht.Ma_BL = Int32.Parse(tempMa_BL);
                hslht.QuyetDinh = tempQuyetDinh;
                hslht.NgayHieuLuc = tempNgayHieuLuc;

                if (buttonEditFileQD.Text.Trim() != "")
                {
                    FileStream frm;
                    frm = new FileStream(@buttonEditFileQD.Text.Trim(), FileMode.Open, FileAccess.Read);
                    byte[] picbyte = new byte[frm.Length];
                    frm.Read(picbyte, 0, System.Convert.ToInt32(frm.Length));
                    frm.Close();
                    frm.Dispose();
                    cbnl.FileQuyetDinh = picbyte;

                }
                else
                {
                    byte[] picbyte = new byte[0];
                    cbnl.FileQuyetDinh = picbyte;

                }

                if (demsua == 1)
                {

                    //========add vao hsl hien tai=========
                    int kq1;
                    kq1 = sv.addHSLHienTai(hslht);
                    if (kq1 == -1)
                    {
                        sv.updateHSLHienTai(hslht);
                    }

                    DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật thông tin ngạch '" + lke_MaNgach.Text + "' bậc '" + lke_MaBL.Text + "' của cán bộ '" + lke_MaCB.Text + "'?", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dr == DialogResult.OK)
                    {
                        sv.updateCBNL(cbnl);
                        LoadData();
                        demsua = 0;
                        MessageBox.Show("Cập nhật thông tin ngạch '" + lke_MaNgach.Text + "' của cán bộ '" + lke_MaCB.Text + "' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    //===========add vao can bo ngach luong===========
                    int kq1;
                    kq1 = sv.addHSLHienTai(hslht);
                    if (kq1 == -1)
                    {
                        sv.updateHSLHienTai(hslht);
                    }
                    
                    int kq;
                    kq = sv.addCBNL(cbnl);
                    if (kq != -1)
                    {
                        LoadData();
                        MessageBox.Show("Tăng ngạch lương '"+lke_MaNgach.Text+"' của cán bộ '"+lke_MaCB.Text+"' thành công lên bậc '"+lke_MaBL.Text+"'!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     }
                    else
                    {
                        MessageBox.Show("Thông tin đã tồn tại! Vui lòng xem lại!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
            
            DialogResult dr = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này?!","CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                float a= float.Parse(mahsl);
                //sv.deleteCBNL(macb, mang, mabl, a);
                LoadData();
                MessageBox.Show("Xóa thành công!");

            }

        }


        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            demsua = 1;

            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;


            txt_QuyetDinh.Enabled = true;
            de_NgayKy.Enabled = true;
            txtNoiDung.Enabled = true;
            de_NgayHieuLuc.Enabled = true;
           
        }

        

        private void uc_ThemQDNgachBacHeso_Load(object sender, EventArgs e)
        {
            LoadData();

            lke_MaNgach.Enabled = true;
            lke_MaBL.Enabled = true;
            txt_QuyetDinh.Enabled = true;
            de_NgayKy.Enabled = true;
            txtNoiDung.Enabled = true;
            de_NgayHieuLuc.Enabled = true;

            if (FrmMain.isBLDauTien == true && Frm_dsSAPTANGBACLUONG.isSapTangBL == false)
            {
                //MessageBox.Show("lay tu frm main"+ FrmMain.sendMa_CB_CBNL);
                lke_MaCB.EditValue = FrmMain.sendMa_CB_CBNL;
                txt_BLHienTai.Text = "0";
                lke_MaBL.EditValue = Int32.Parse(txt_BLHienTai.Text) + 1;
                txt_BLHienTai.Enabled = false;
                lke_MaBL.Enabled = false;
            }
            if (Frm_dsSAPTANGBACLUONG.isSapTangBL == true && FrmMain.isBLDauTien == false)
            {
                //MessageBox.Show("lay tu danh sach sap tang luong"+ Frm_dsSAPTANGBACLUONG.sendMa_CB_CBSapTangNL);
                lke_MaCB.EditValue = Frm_dsSAPTANGBACLUONG.sendMa_CB_CBSapTangNL;
                
                string a = Frm_dsSAPTANGBACLUONG.sendMa_CB_CBSapTangNL;

                lke_MaBL.Enabled = false;
                List<QLNSServiceReference.BAC_LUONG> listBLtheoCB = new List<QLNSServiceReference.BAC_LUONG>(sv.getBLtheoCB(a));
                int blhientai = listBLtheoCB[0].Ma_BL;
                txt_BLHienTai.Text = blhientai.ToString();
                lke_MaBL.EditValue = Int32.Parse(txt_BLHienTai.Text) + 1;

                lke_MaNgach.Enabled = false;
                List<QLNSServiceReference.NGACH> listNgachtheoCB = new List<QLNSServiceReference.NGACH>(sv.getNgachTheoCB(a));
                string mangachtheoCB = listNgachtheoCB[0].Ma_Ngach;
                lke_MaNgach.EditValue = mangachtheoCB;
                //MessageBox.Show(mangachtheoCB);

                lke_MaNgach.Enabled = false;
                txt_BLHienTai.Enabled = false;
                lke_MaBL.Enabled = false;
            }
        }


        public void LoadData()
        {
            btn_Them.Enabled = false;
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            lke_MaCB.Enabled = false;
            lke_MaNgach.Enabled = false;
            lke_MaBL.Enabled = false;
            txt_QuyetDinh.Enabled = false;
            de_NgayKy.Enabled = false;
            txtNoiDung.Enabled = false;
            de_NgayHieuLuc.Enabled = false;
           
            lke_MaCB.Properties.DataSource = sv.getCB();
            lke_MaNgach.Properties.DataSource = sv.getNgach();
            lke_MaBL.Properties.DataSource = sv.getBL();     

            gv_dsQD.DataSource = sv.getCBNL();
            gridView1.Columns[0].Caption = "Mã cán bộ";
            gridView1.Columns[1].Caption = "Mã ngạch lương";
            gridView1.Columns[2].Caption = "Mã bậc lương";
            gridView1.Columns[3].Caption = "Quyết định số";
            gridView1.Columns[4].Caption = "Nội dung";
            gridView1.Columns[5].Caption = "Ngày ký";
            gridView1.Columns[6].Caption = "Ngày hiệu lực";
            gridView1.Columns[7].Visible = false;

            if (gridView1.DataRowCount < 0)
            {
                MessageBox.Show("Chưa có dữ liệu!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
           }

        
        
       
        private void btn_Themngach_Click(object sender, EventArgs e)
        {
            Frm_ThemNgach frm = new Frm_ThemNgach();
            frm.ShowDialog();
            lke_MaNgach.Properties.DataSource = sv.getNgach();
        }


        string tempLoaiNgach;

        private void lke_MaNgach_EditValueChanged(object sender, EventArgs e)
        {
            if (lke_MaNgach.EditValue.ToString() =="")
            {
                lke_MaBL.EditValue = "";
            }
            else
            {
                string chonNgach = lke_MaNgach.EditValue.ToString();
                List<QLNSServiceReference.NGACH> listng = new List<QLNSServiceReference.NGACH>(sv.getMaLoaitheoMaNgach(chonNgach));

                tempLoaiNgach = listng[0].Ma_Loai_Ngach;

                lke_MaBL.Properties.DataSource = sv.getBLHSLtheoLNG(tempLoaiNgach);
            }
            //if (lke_MaNgach.EditValue != "")
            //{
            //    string chonNgach = lke_MaNgach.EditValue.ToString();
            //    List<QLNSServiceReference.NGACH> listng = new List<QLNSServiceReference.NGACH>(sv.getMaLoaitheoMaNgach(chonNgach));

            //    tempLoaiNgach = listng[0].Ma_Loai_Ngach;

            //    lke_MaBL.Properties.DataSource = sv.getBLHSLtheoLNG(tempLoaiNgach);
            //}
        }

        float tempHSL;
        private void lke_MaBL_EditValueChanged(object sender, EventArgs e)
        {
            if (lke_MaBL.EditValue.ToString() != "")
            {
                string chonBL = lke_MaBL.EditValue.ToString();
                List<QLNSServiceReference.HESO_LUONG> listHSL = new List<QLNSServiceReference.HESO_LUONG>(sv.getHSLtheoBL(tempLoaiNgach, chonBL));
                tempHSL = listHSL[0].Ma_HS_Luong;
                //MessageBox.Show(tempHSL.ToString());
            }
            //if (lke_MaNgach.EditValue != "")
            //{
            //   if (lke_MaBL.EditValue != "")
            //    {
            //        string chonBL = lke_MaBL.EditValue.ToString();
            //        List<QLNSServiceReference.HESO_LUONG> listHSL = new List<QLNSServiceReference.HESO_LUONG>(sv.getHSLtheoBL(tempLoaiNgach, chonBL));
            //        tempHSL = listHSL[0].Ma_HS_Luong;
            //        //MessageBox.Show(tempHSL.ToString());
            //    }
            //}
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            lke_MaCB.Enabled = true;
            lke_MaNgach.Enabled = false;
            lke_MaBL.Enabled = false;

           
            txt_QuyetDinh.Enabled = true;
            de_NgayKy.Enabled = true;
            txtNoiDung.Enabled = true;
            de_NgayHieuLuc.Enabled = true;
           

          
            
            //lke_MaNgach.EditValue = "";                     
            //lke_MaBL.EditValue = "";
            txt_QuyetDinh.Text = "";
            txtNoiDung.Text = "";
            de_NgayKy.Text = "";
            de_NgayHieuLuc.Text = "";
           
        }

       
        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;
            
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            
                                   
            lke_MaCB.Enabled = false;
            lke_MaNgach.Enabled = false;
            lke_MaBL.Enabled = false;
            txt_QuyetDinh.Enabled = false;
            de_NgayKy.Enabled = false;
            txtNoiDung.Enabled = false;
            de_NgayHieuLuc.Enabled = false;
           }

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            Frm_ThemBL frm = new Frm_ThemBL();
            frm.ShowDialog();
            lke_MaBL.Properties.DataSource = sv.getBL();
        }

        
       

        XtraFormPdfViewer frmpdf;
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                if ((byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[7]) != null && ((byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[7])).Length != 8000)
                    if (frmpdf == null || frmpdf.IsDisposed)
                    {

                        byte[] pdfbyte = (byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[7]);

                        if (pdfbyte.Length != 0)
                        {
                            frmpdf = new XtraFormPdfViewer(pdfbyte);
                            frmpdf.Show();
                        }
                        else
                        {
                            frmpdf.Activate();
                        }
                    }
            }
        }


        XtraFormPdfViewer fqd;
        private void buttonEditFileQD_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {

                FileDialog fdg = new OpenFileDialog();
                fdg.InitialDirectory = @"d:\";
                fdg.Filter = "File PDF(*.pdf)|*pdf";
                if (fdg.ShowDialog() == DialogResult.OK)
                {

                    buttonEditFileQD.Text = fdg.FileName;
                    if (fqd == null || fqd.IsDisposed)
                    {
                        fqd = new XtraFormPdfViewer(fdg.FileName);
                        fqd.Show();
                    }
                    else
                        fqd.Activate();
                }
                fdg = null;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }


        string macb = "";
        string mang = "";
        string mabl = "";
        string mahsl = "";
   
        private void gv_dsQD_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;



            lke_MaCB.Enabled = false;
            lke_MaNgach.Enabled = false;
            lke_MaBL.Enabled = false;
            txt_QuyetDinh.Enabled = false;
            de_NgayKy.Enabled = false;
            txtNoiDung.Enabled = false;
            de_NgayHieuLuc.Enabled = false;
            buttonEditFileQD.Enabled = false;

            try
            {

                lke_MaCB.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                lke_MaNgach.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                //lke_MaBL.EditValue = Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]));
                txt_QuyetDinh.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);
                txtNoiDung.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[4]);
                de_NgayKy.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[5]));
                de_NgayHieuLuc.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[6]));

                macb = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                mang = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                mabl = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                mahsl = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);


                //----------------------lay ten don vi----------------------------

                //List<QLNSServiceReference.DANHMUC_DONVI> listTenDVtheoMaCB = new List<QLNSServiceReference.DANHMUC_DONVI>(sv.getTenDVtheoMaCB(macb));
                //string a = listTenDVtheoMaCB[0].Ma_DV;
                //lookUpEdit1.EditValue = a;

                //------------------------//---------------------



            }
            catch { }


        }

        private void lke_MaCB_EditValueChanged(object sender, EventArgs e)
        {
            string a = lke_MaCB.EditValue.ToString();
            int kq = sv.checkQDCBNL(a);
            if (kq != -1)
            {
                txt_BLHienTai.Text = "0";
                lke_MaBL.EditValue = Int32.Parse(txt_BLHienTai.Text) + 1;
                lke_MaNgach.EditValue = "";
                txt_BLHienTai.Enabled = false;
                lke_MaBL.Enabled = false;
            }
            else
            {
                lke_MaBL.Enabled = false;
                List<QLNSServiceReference.BAC_LUONG> listBLtheoCB = new List<QLNSServiceReference.BAC_LUONG>(sv.getBLtheoCB(a));
                int blhientai = listBLtheoCB[0].Ma_BL;
                txt_BLHienTai.Text = blhientai.ToString();
                lke_MaBL.EditValue = Int32.Parse(txt_BLHienTai.Text) + 1;

                lke_MaNgach.Enabled = false;
                List<QLNSServiceReference.NGACH> listNgachtheoCB = new List<QLNSServiceReference.NGACH>(sv.getNgachTheoCB(a));
                string mangachtheoCB = listNgachtheoCB[0].Ma_Ngach;
                lke_MaNgach.EditValue = mangachtheoCB;
               //MessageBox.Show(mangachtheoCB);

                lke_MaNgach.Enabled = false;
                txt_BLHienTai.Enabled = false;
                lke_MaBL.Enabled = false;
            }
        }

       
       

     
    }
}
